layui.use(['form', 'jquery', 'layer', 'table', 'laydate', 'laypage'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var laypage = layui.laypage;
	var checkStatus = new Array;

	var table_filter = 'goods_copy_list',
		form_filter = 'goods_copy_search_form';
	// 渲染表格
	table.render({
		elem: '#' + table_filter,
		id: table_filter,
		url: goods_list_url,
		method: 'post',
		height: 'full-200',
		where: {
			sale_state: 1,
			audit_state: 1,
			is_copy: 1,
			category_sign3: category_all_sign,
			_o: 'goods_id',
			_b: 'desc'
		},
		cols: [goods_cols],
		page: {
			layout: ['count', 'first', 'prev', 'page', 'next', 'last', 'skip'],
			limit: 15,
			theme: '#393d49',
			groups: 8
		},
		autoSort: false,
		toolbar: '#goodsToolbar',
		defaultToolbar: ['filter'],
		initSort: {
			field: 'goods_id',
			type: 'desc'
		}
	});
	table.on('checkbox(' + table_filter + ')', function(obj) {
		checkData = table.checkStatus(table_filter);
		checkStatus = [];
		for(var i in checkData.data) {
			checkStatus.push(checkData.data[i]['goods_sku']);
		}
	})
	// 监听表头按钮
	table.on('toolbar(' + table_filter + ')', function(obj) {
		switch(obj.event) {
			case 'copyGoods':
				if(checkStatus.length == 0) {
					layer.msg('请选择要同步的商品', {
						icon: 2,
						anim: 6
					});
					return false;
				}
				var field = {
					select_site_id: select_site_id,
					goods_sku: checkStatus
				};
				load_ing = layer.load(1);
				send_info(goods_save_copy_url, field, function(data) {
					layer.close(load_ing);
					if(data.code == 0) {
						layer.msg(data.data, {
							icon: 2,
							anim: 6
						});
						return false;
					} else {
						layer.msg(data.data, {
							icon: 1,
							anim: 6
						});
						layui.table.reload('goods_copy_list');
						parent.layui.table.reload('goods_list');
					}
				})
				break;
			default:
				break;
		}
		return false;
	})

	// 排序
	table.on('sort(' + table_filter + ')', function(data) {
		var _o = data.field;
		var _b = data.type;
		table.reload(table_filter, {
			initSort: data,
			where: {
				_o: _o,
				_b: _b
			}
		})
	})
	form.render();
	// 表单input 查询
	$('form[lay-filter="' + form_filter + '"]').on('input propertychange', 'input[type="text"]', function() {
		if($(this).prop('comStart')) return;
		var value = $(this).val();
		var name = $(this).attr('name');
		var where = {};
		where[name] = value;
		table.reload(table_filter, {
			where: where,
			page:{
				curr:1
			}
		});
	}).on('compositionstart', 'input[type="text"]', function() {
		$(this).prop('comStart', true);
	}).on('compositionend', 'input[type="text"]', function() {
		$(this).prop('comStart', false);
		$(this).trigger('input');
	})
	var edit_brand = function(field, value, brand_sign) {
		var post = '{"brand_sign":"' + brand_sign + '","' + field + '":"' + value + '"}';
		var data = JSON.parse(post);
		send_info(brand_save_url, data, function(data) {
			if(data.code == -1) {
				layer.msg(data.msg, {
					icon: 2,
					anim: 6
				});
			} else {
				layer.msg('修改成功', {
					icon: 1
				});
			}
		})
	}
	//监听编辑
	table.on('edit(' + table_filter + ')', function(obj) {
		var field = obj.field,
			value = obj.value,
			brand_sign = obj.data.brand_sign;
		edit_brand(field, value, brand_sign);
	});
	// 表格工具监听
	table.on('tool(' + table_filter + ')', function(obj) {
		var data = obj.data;
		var e = obj.event;
		var tr = obj.tr;
		var brand_sign = data.brand_sign;
		var brand_id = data.brand_id;
		switch(e) {
			case 'del':
				// 删除
				layer.confirm('确定删除吗？', function(index) {

					send_info(brand_delete_url, {
						brand_id: brand_id
					}, function(data) {
						if(data.code == 0) {
							obj.del();
							layer.msg('删除成功', {
								icon: 1
							});
							// 重载回收站
							if($('table[lay-filter="recycle_brand_list"]').length > 0) {
								table.reload('recycle_brand_list');
							}
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					})
				})
				break;
			case 'edit':
				create_tab($(this));
				break;
			case 'update':
				send_info(brand_one_url, {
					brand_sign: data.brand_sign
				}, function(data) {
					delete data.data.brand_id;
					obj.update(data.data);
				})
				break;
			default:
				break;
		}
	});

	// 表单input 查询
	$('form[lay-filter="' + form_filter + '"]').on('input propertychange', 'input[type="text"]', function() {
		if($(this).prop('comStart')) return;
		var value = $(this).val();
		var name = $(this).attr('name');
		var where = {};
		where[name] = value;
		table.reload(table_filter, {
			where: where,
			page:{
				curr:1
			}
		});
	}).on('compositionstart', 'input[type="text"]', function() {
		$(this).prop('comStart', true);
	}).on('compositionend', 'input[type="text"]', function() {
		$(this).prop('comStart', false);
		$(this).trigger('input');
	});
	var select_search_copy = function(field, value) {
		var where = {};
		where[field] = value;
		table.reload(table_filter, {
			where: where,
			page:{
				curr:1
			}
		});
	};
	// 获取品牌
	var get_goods_brand_copy = function(category_sign) {
		var e = $('form[lay-filter="' + form_filter + '"]').find('#brand_sign');
		e.html('');
		e.append('<option value="">加载中...</option>');
		e.prop('disabled', true);
		form.render('select');
		var post_data = {
			is_select: 'yes',
			category_sign: category_sign,
			_o: 'brand_id',
			_b: 'desc',
			page: 1,
			limit: 500
		};
		send_info(brand_select_uri, post_data, function(data) {
			e.html('<option value="">请选择</option>');
			if(data.count > 0) {
				for(var i in data.data) {
					var temp = data.data[i];
					if(goods_form_value.brand_sign == temp.brand_sign) {
						var op_html = '<option selected value="' + temp.brand_sign + '">' + temp.brand_name + '</option>';
					} else {
						var op_html = '<option value="' + temp.brand_sign + '">' + temp.brand_name + '</option>';
					}
					e.append(op_html);
				}
			};
			e.prop('disabled', false);
			form.render('select');
		})
	};
	var goods_form_value = {
		category_sign1: '',
		category_sign2: '',
		category_sign3: ''
	};
	// 初始化分类
	var get_goods_category_copy = function(parent_sign, filter, value) {
		var e = $('form[lay-filter="' + form_filter + '"]').find('#' + filter);
		e.html('');
		e.append('<option value="">加载中...</option>');
		e.prop('disabled', true);
		var id = e.attr('id');
		form.render('select');
		var post_data = {
			parent_sign: parent_sign,
			site_id: select_site_id
		}
		send_info(category_select_uri, post_data, function(data) {
			e.html('<option value="">请选择分类</option>');
			for(var i in data.data) {
				var temp = data.data[i];
				if(value == temp.category_sign) {
					var op_html = '<option selected value="' + temp.category_sign + '">' + temp.category_name + '</option>';
				} else {
					var op_html = '<option value="' + temp.category_sign + '">' + temp.category_name + '</option>';
				}
				e.append(op_html);
			}
			e.prop('disabled', false);
			form.render('select');
			if(value != undefined) {
				if(id == 'category_search1') {
					get_goods_category_copy(value, 'category_search2', goods_form_value.category_sign2);
					$('#category_search3').html('');
					$('#goods_brand_search').html('');
					get_goods_brand_copy(value);
				} else if(id == 'category_search2') {
					get_goods_category_copy(value, 'category_search3', goods_form_value.category_sign3);
					get_goods_brand_copy(value);
				} else {
					get_goods_brand_copy(value);
				}
			}
		})
	}
	var fliter1 = 'category_search1';
	// 分类联动
	form.on('select(copy_category_search1)', function(data) {
		var value = data.value;
		var e = data.elem;
		get_goods_category_copy(value, 'category_search2', goods_form_value.category_sign2);
		$('form[lay-filter="' + form_filter + '"]').find('#category_search3').html('');
		$('form[lay-filter="' + form_filter + '"]').find('#brand_sign').html('');
		get_goods_brand_copy(value);
		select_search_copy('category_sign1', value);
	})
	form.on('select(copy_category_search2)', function(data) {
		var value = data.value;
		var e = data.elem;
		get_goods_category_copy(value, 'category_search3', goods_form_value.category_sign3);
		get_goods_brand_copy(value);
		select_search_copy('category_sign2', value);
	})
	form.on('select(copy_category_search3)', function(data) {
		var value = data.value;
		var e = data.elem;
		get_goods_brand_copy(value);
		select_search_copy('category_sign3', value);
	})
	form.on('select(copy_brand_sign)', function(data) {
		var value = data.value;
		var e = data.elem;
		select_search_copy('brand_sign', value);
	})
	get_goods_category_copy(0, 'category_search1', goods_form_value.category_sign1);
})